<?php

namespace app\common\model;


use app\common\exception\ApiException;
use think\Model;

class BaseModel extends Model {
    protected $autoWriteTimestamp = true;

    public function add($data) {
        if (empty($data['status'])) {
            $data['status'] = config('code.status_show');
        }
        try {
            $this->allowField(true)->save($data);
        } catch (\Exception $e) {
            throw new ApiException($e->getMessage(), 400, 1);
        }
        return $this->getLastInsID();
    }

    public function edit($data, $id) {
        unset($data['id']);
        try {
            $this->allowField(true)->save($data, ['id' => $id]);
        } catch (\Exception $e) {
            throw new ApiException($e->getMessage(), 400, 1);
        }
        return $id;
    }
}